<?php
/**
 *
 */
class M_brand extends CI_Model {

  public function __construct() {
    // parent::__construct();
    $this->load->database();
  }
  public function getBrandlist($count,$offset){
    $sql = 'select * from brand order by bid limit '.$offset.','.$count;
    $query = $this->db->query($sql);
    $total = $this->db->query('select * from brand');
    return array(
      'brandlist' => $query->result_array(),
      'total'    => $total->num_rows(),
      'totallist'=> $total->result_array()
    );
  }
  public function getBrand($key,$count,$offset){
    $key = '"%'.$key.'%"';
    $sql = 'select * from brand where bid like '.$key.' or bname like '.$key.
    ' limit '.$offset.','.$count;
    $query = $this->db->query($sql);
    $total = $this->db->query('select * from brand where bid like '.$key.' or bname like '.$key);
    return array(
      'brandlist' => $query->result_array(),
      'total'    => $total->num_rows(),
      'totallist'=> $total->result_array()
    );
  }
  public function addBrand($val){
    $sql = 'insert into brand(bname) values("'.$val.'")';
    return $this->db->simple_query($sql);
  }
  public function updateBrand($bid,$val){
    $obname = $this->db->query('select bname from brand where bid='.$bid)->result_array();
    $obname = $obname[0]['bname'];
    $sql = 'update brand set bname="'.$val.'" where bid='.$bid.'';
    $this->db->simple_query($sql);
    $result = $this->db->query('select * from brandshop where bid='.$bid)->result_array();
    foreach ($result as $shop):
      $re = $this->db->query('select shopbrand from shops where shopid='.$shop['shopid'])->result_array();
      $b = explode(',',$re[0]['shopbrand']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$obname){
          $b[$i]=$val;
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update shops set shopbrand = "'.$str.'" where shopid='.$shop['shopid']);
    endforeach;

    $result = $this->db->query('select * from tebrand where bid='.$bid)->result_array();
    foreach ($result as $terminal):
      $re = $this->db->query('select bid from terminal where teid='.$terminal['teid'])->result_array();
      $b = explode(',',$re[0]['bid']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$obname){
          $b[$i]=$val;
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update terminal set bid = "'.$str.'" where teid='.$terminal['teid']);
    endforeach;
    return '1';
    // return $this->db->simple_query($sql);
  }
  public function deleteBrand($bid){
    $obname = $this->db->query('select bname from brand where bid='.$bid)->result_array();
    $obname = $obname[0]['bname'];
    $sql = 'delete from brand where bid='.$bid.'';
    $result = $this->db->query('select * from brandshop where bid='.$bid)->result_array();
    foreach ($result as $shop):
      $re = $this->db->query('select shopbrand from shops where shopid='.$shop['shopid'])->result_array();
      $b = explode(',',$re[0]['shopbrand']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$obname){
          array_splice($b,$i,1);
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update shops set shopbrand = "'.$str.'" where shopid='.$shop['shopid']);
    endforeach;

    $result = $this->db->query('select * from tebrand where bid='.$bid)->result_array();
    foreach ($result as $terminal):
      $re = $this->db->query('select bid from terminal where teid='.$terminal['teid'])->result_array();
      $b = explode(',',$re[0]['bid']);
      $str = '';
      for ($i=0; $i <sizeof($b) ; $i++) {
        if($b[$i]==$obname){
          array_splice($b,$i,1);
        }
      }
      for ($i=0;$i<sizeof($b);$i++){
        if($i==0){
          $str = $str.$b[$i];
        }else{
          $str = $str.','.$b[$i];
        }
      }
      $this->db->simple_query('update terminal set bid = "'.$str.'" where teid='.$terminal['teid']);
    endforeach;

    $this->db->simple_query($sql);
    $this->db->simple_query('delete from brandshop where bid='.$bid);
    $this->db->simple_query('delete from summary where bid='.$bid);
    return '1';
    // return $this->db->simple_query($sql);
  }
  public function getallbrand($count,$offset){
    // $sql = 'select * from brand';
    $sql = 'select * from brand limit '.$offset.','.$count;
    $total = $this->db->query('select * from brand');
    return array(
      'brandlist' => $this->db->query($sql)->result_array(),
      'total'    => $total->num_rows()
    );
  }
  public function getallbrandselect(){
    $sql = 'select * from brand';
    return $this->db->query($sql)->result_array();
  }
  public function getshopbrand($shopid){
    $sql = 'select brandshop.bid,bname from brandshop,brand where brand.bid=brandshop.bid and shopid='.$shopid;
    return $this->db->query($sql)->result_array();
  }
}
